package com.emapp.lccode.activity

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import com.emapp.lccode.R
import com.emapp.lccode.adapter.MaterialAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.BaseConfig
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.MaterialKt
import com.emapp.lccode.databinding.ActivityWuliaoBinding
import com.emapp.lccode.mode.OrderKtViewModel
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.emapp.lccode.utils.vertical
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.Toaster
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import me.hgj.jetpackmvvm.ext.parseState
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class WuLiaoActivity : BaseActivity1<OrderKtViewModel, ActivityWuliaoBinding>() {
    private val materialAdapter: MaterialAdapter by lazy { MaterialAdapter() }
    private var keywords = ""
    var page = 1
    var datas: ArrayList<MaterialKt> = arrayListOf()
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "物料"
        mViewBind.lvTitle.tvRight.visibility = View.GONE
        EventBus.getDefault().register(this)
        mViewBind.rvList.run {
            vertical()
            adapter = materialAdapter
        }
        mViewBind.refreshLayout.run {
            setRefreshHeader(ClassicsHeader(this@WuLiaoActivity))
            setRefreshFooter(ClassicsFooter(this@WuLiaoActivity))
            setOnRefreshListener {
                page = 1
                mViewModel.getWuLiaoList(keywords, page)
            }
            setOnLoadMoreListener {
                page++
                mViewModel.getWuLiaoList(keywords, page)
            }
        }
        mViewBind.etSearch.setOnEditorActionListener { textView, actionId, keyEvent ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                keywords = textView.text.toString()
                page = 1
                showLoading()
                mViewModel.getWuLiaoList(keywords, page)
            }
            false
        }
        materialAdapter.setOnItemClickListener { adapter, _, position ->
            val item = adapter.items[position]
            EventBus.getDefault().post(EventBusModel(EventBusConfig.SELECT_MATERIAL,item))
            finish()
        }
        showLoading()
        mViewModel.getWuLiaoList("", page)
    }

    override fun createObserver() {
        mViewModel.run {
            mateial.observe(this@WuLiaoActivity) { resultState ->
                parseState(resultState, {
                    if (page == 1) {
                        datas.clear()
                        if (it?.data != null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishRefresh()
                            }
                        } else {
                            mViewBind.refreshLayout.finishRefreshWithNoMoreData()
                        }
                    } else {
                        if (it?.data != null) {
                            datas.addAll(it.data)
                            if (it.data.size < BaseConfig.page_count) {
                                mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                            } else {
                                mViewBind.refreshLayout.finishLoadMore()
                            }
                        } else {
                            mViewBind.refreshLayout.finishLoadMoreWithNoMoreData()
                        }
                    }
                    materialAdapter.submitList(datas)
                    materialAdapter.notifyDataSetChanged()
                }, {
                    Toaster.show(it.errorMsg)
                })

            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight,
            mViewBind.ivSearch
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.iv_search -> {
                    keywords = mViewBind.etSearch.getText().toString().trim()
                    page = 1
                    showLoading()
                    mViewModel.getWuLiaoList(keywords, page)
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }

}
